package co.crossoft.exposeServiceImpl;

import co.crossoft.daoImpl.BomDaoImpl;
import co.crossoft.domain.BomBean;
import co.crossoft.domain.BomComponentBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import service.ExposeBomService;

import java.math.BigDecimal;

/**
 * Created by Snow on 8/10/2017.
 */
@Component
public class ExposeBomServiceImpl implements ExposeBomService {

    @Autowired
    BomDaoImpl bomDaoImpl;


    @Override
    public String bomListByBom(String plant,String id,String version) {
        String retString = bomDaoImpl.getBomList(plant,id,version);
        return retString;
    }

    @Override
    public String addBomData(String plant,
                               String id,
                               String adopted,
                               String bomType,
                               String curVersion,
                               String version,
                               String name,
                               String bomTem,
                               String statusFk,
                               String dateEffect,
                               String dateExpire)
    {
        BomBean bomBean = new BomBean();
        bomBean.setQuid("BOM:"+plant+","+id+","+version);
        bomBean.setPlant(plant);
        bomBean.setId(id);
        bomBean.setAdopted(adopted);
        bomBean.setBomType(bomType);
        bomBean.setCurVersion(curVersion);
        bomBean.setVersion(version);
        bomBean.setName(name);
        bomBean.setBomTem(bomTem);
        bomBean.setStatusFk(statusFk);
        bomBean.setDateEffect(dateEffect);
        bomBean.setDateExpire(dateExpire);
        String retString = bomDaoImpl.addOrUpdateBom(bomBean);
        return retString;
    }

    @Override
    public String deleteBomData(String plant,String id,String version) {
        String retString = bomDaoImpl.deleteBomData(plant,id,version);
        return retString;
    }

    @Override
    public String deleteBomAndComp(String quid){
        String retString = bomDaoImpl.deleteBomAndComp(quid);
        return retString;
    }

    @Override
    public String addComponent( String sequence,
                                String assembleOperationFk,
                                String bomFk,
                                String bomComponentType,
                                String disassembleOperationFk,
                                String assyDataTypeFk,
                                String maximumNc,
                                String materialFk,
                                String maximumUsage,
                                String qty,
                                String assembleAsReq,
                                String enabled,
                                String useItemDefaults,
                                String testPart) {
        BomComponentBean componentBean =  new BomComponentBean();
        componentBean.setId("BOMCOM:"+materialFk+","+bomFk+","+sequence);
        componentBean.setSequence(new BigDecimal(sequence));
        componentBean.setAssembleOperationFk(assembleOperationFk);
        componentBean.setBomFk(bomFk);
        componentBean.setBomComponentType(bomComponentType);
        componentBean.setDisassembleOperationFk(disassembleOperationFk);
        componentBean.setAssyDataTypeFk(assyDataTypeFk);
        componentBean.setMaximumNc( Double.parseDouble(maximumNc) );
        componentBean.setMaximumUsage( Double.parseDouble(maximumUsage) );
        componentBean.setMaterialFk(materialFk);
        componentBean.setQty( Double.parseDouble(qty) );
        componentBean.setAssembleAsReq(assembleAsReq);
        componentBean.setEnabled(enabled);
        componentBean.setUseItemDefaults(useItemDefaults);
        componentBean.setTestPart(testPart);
        String retString = bomDaoImpl.addOrUpdateComponent(componentBean);
        return retString;
    }

    @Override
    public String deleteComponent(String bomComponentIdJson) {
        String retString = bomDaoImpl.deleteBomComponent(bomComponentIdJson);
        return retString;
    }

    @Override
    public String reDisBomComp(String souBomCompId,String destBomId) {
        String retString = bomDaoImpl.reDistributeBomComponent(souBomCompId,destBomId);
        return retString;
    }

    @Override
    public String getBomComponent(String id,String bomFk) {
        String retString = bomDaoImpl.getBomComponentList(id,bomFk);
        return retString;
    }

    @Override
    public String createNewVisionBom(String id, String name, String version, String parentBomId,String plant) {
        String retString = bomDaoImpl.createVirMaterial(id,name,version,parentBomId,plant);
        return retString;
    }

    @Override
    public String dialogComSelect(String quid, String id) {
        String retString = bomDaoImpl.dialogComSelect(quid,id);
        return retString;
    }
}
